//
// Created by Win10 on 2022/10/30.
//
#include<iostream>
#include "cl_queue.h"

using namespace std;

bool InitQueue(ClQueue &Q, int space) {
    if (space < 1)return false;
    Q.size = 0;
    Q.front = Q.rear = (LinkNode *) malloc(sizeof(LinkNode));
    Q.rear->next = nullptr;
    for (int i = 0; i < space - 1; i++) {
        auto *s = (LinkNode *) malloc(sizeof(LinkNode));
        s->next = nullptr;
        Q.rear->next = s;
        Q.rear = s;
    }
    Q.rear->next = Q.front;
    Q.rear = Q.front;
    return true;
}

bool DestroyQueue(ClQueue &Q) {
    if (Q.front == nullptr)return false;
    while (Q.front->next != Q.front) {
        LinkNode *s = Q.front->next;
        Q.front->next = Q.front->next->next;
        free(s);
    }
    free(Q.front);
    Q.front = Q.rear = nullptr;
    Q.size = 0;
    return true;
}

bool QueueEmpty(ClQueue Q) {
    return Q.size == 0;
}

bool QueueFull(ClQueue Q) {
    return Q.rear == Q.front && !QueueEmpty(Q);
}

bool EnQueue(ClQueue &Q, ElemType x) {
    if (QueueFull(Q)) return false;
    Q.rear->data = x;
    Q.rear = Q.rear->next;
    Q.size++;
    return true;
}

bool DeQueue(ClQueue &Q, ElemType &x) {
    if (QueueEmpty(Q)) return false;
    x = Q.front->data;
    Q.front = Q.front->next;
    Q.size--;
    return true;
}

bool GetHead(ClQueue Q, ElemType &x) {
    if (QueueEmpty(Q)) return false;
    x = Q.front->data;
    return true;
}

int GetSize(ClQueue Q) {
    return Q.size;
}

int main() {
    {
        ClQueue Q;
        int space_size=5;
        InitQueue(Q,space_size);

        for (int i = 1; i <= 5; i++) {
            bool f = EnQueue(Q, i);
            if (!f) cout << i << endl;
        }
        ElemType x;
        DeQueue(Q, x);
        cout << x << endl;
        DeQueue(Q, x);
        cout << x << endl;
        EnQueue(Q, 1);
        EnQueue(Q, 2);
        cout << Q.front << " " << Q.rear << endl;
        if (!EnQueue(Q, 6)) {
            cout << "队列已满" << endl;
        }
        GetHead(Q, x);
        cout << x << endl << endl;
        while (!QueueEmpty(Q)) {

            DeQueue(Q, x);
            cout << x << endl;
        }
    }
    return 0;
}